var mixin = {
    methods: {
        // 上传开始
        uploadNewFile(newFile) {
            this.uploadFileArr.push(newFile);
        },
        // 上传中
        uploadNext({ token, percent }) {
            let file = this.uploadFileArr.find(ele => token === ele.token);
            file.uploadPercent = parseInt(percent);
        },
        // 上传完成
        uploadComplete({ token, url }) {
            let file = this.uploadFileArr.find(ele => token === ele.token);
            file.fileUrl = url;
        },
        // 上传失败
        uploadError({ token, err }) {
            console.log(err);
            let file = this.uploadFileArr.find(ele => token === ele.token);
            file.uploadPercent = -1;
        },
        // 终止上传
        finishUploadFile(token) {
            this.$confirm(`确定终止上传并删除此附件吗？`, `提示`, {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    this.$refs.upload.unsubscribe(token);
                    this.deleteFile(token);
                    console.log("取消上传");
                })
                .catch(err => console.log(err));
        },
        // 删除已上传
        deleteFile(token) {
            let index = this.uploadFileArr.findIndex(
                ele => token === ele.token
            );
            this.uploadFileArr.splice(index, 1);
        },
        // 重新上传
        uploadFileAgain(token) {
            this.deleteFile(token);
            this.$refs.upload.uploadFileAgain(token);
        },
        // 判断所有附件是否上传完成
        allUploadComplete() {
            let allComplete = !this.uploadFileArr.some(
                ele => ele.uploadPercent < 100
            );
            if (!allComplete) {
                this.$alert("附件还没有上传完成");
            }
            return allComplete;
        }
    }
};
export default mixin;
